package com.example.birdfriends.utils.recommend;

import com.example.birdfriends.pojo.post.Post;
import com.example.birdfriends.pojo.praise.Praise;
import com.example.birdfriends.pojo.recommend.Recommend;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.service.*;
import com.example.birdfriends.utils.convert.ConvertService;
import com.google.j2objc.annotations.AutoreleasePool;
import javafx.geometry.Pos;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class RecommendUtil {
    // 推荐工具类
    @Autowired
    private UserService userService;
    @Autowired
    private PraiseService praiseService;
    @Autowired
    private PostService postService;

    @PostConstruct
    public static void setUserPraisesMatrix(){
        // 设置用户点赞矩阵

    }

    @Autowired
    private ReadService readService;
    @Autowired
    private RecommendService recommendService;

    public List<Long> collaborativeFilter(Long curLoginUid){
        /* 设置用户点赞矩阵  */
        long userNumber = userService.countUserNumber();  // 用户个数
        long postNumber = postService.countPostsNotLogin();  // 登录状态下的贴子数（不包括当前登录用户所发表的帖子）

        System.out.println("post number:"+postNumber+"; user number:"+userNumber);
        // 用户id列表
        List<Long> userIdList = userService.findUserIdsOrderById();  // 所有用户的id列表
        // 帖子id列表
        // 不能过滤用户自己发表的帖子，因为用户可能点赞自己发表的帖子，过滤的话就会数据越界
        List<Long> postIdList = postService.findPostIdsOrderByPid();  //
        // 点赞列表
        List<Praise> praiseList = praiseService.findPraises();
        // 构建用户对帖子的点赞矩阵
        int[][] userPraises = new int[(int) userNumber][(int) postNumber];
        for (Praise praise: praiseList){
            // 将数据插入矩阵
            Long userId = praise.getUid(), postId = praise.getPid();
            int rowIndex = userIdList.indexOf(userId), colIndex = postIdList.indexOf(postId);
            userPraises[rowIndex][colIndex] = 1;
        }

        /*  查询与当前用户点赞了相同帖子的用户  */
        int curLoginUidIndex = userIdList.indexOf(curLoginUid);  // 当前登录用户在矩阵中的下标索引
        List<Long> otherUserIdsWithSimilarInterest = new ArrayList<>();  // 有着相似兴趣的其他用户
        for(int i = 0; i < userPraises[curLoginUidIndex].length; i++){
            // 遍历当前用户的点赞情况
            if(userPraises[curLoginUidIndex][i] == 1){
                // 用户点赞了当前的帖子
                Long pid = postIdList.get(i);  // 当前用户点赞的pid
                // 通过这篇帖子找到其他点赞了这篇帖子的用户id
//                List<Long> otherPraiseUids =
                int pidIndex = postIdList.indexOf(pid);
                for (int userIndex = 0; userIndex < userNumber; userIndex++){
                    if (userIndex != curLoginUidIndex && userPraises[userIndex][pidIndex] == 1){
                        // 遍历到的用户不是当前登录用户，并且当前用户点赞了当前帖
                        if (!otherUserIdsWithSimilarInterest.contains(userIdList.get(userIndex))){
                            otherUserIdsWithSimilarInterest.add(userIdList.get(userIndex));
                        }
                    }
                }
            }
        }

        /*  找出这些有着相似兴趣的用户所点赞，而当前用户没有点赞的帖子  */
        List<Long> recommendPidList = new ArrayList<>();
        List<Long> curUserReadPidList = readService.findUserReadPostIds(curLoginUid);   // 当前登录用户已阅读过的帖子
        List<Long> curUserPidList = new ArrayList<>();
        for (Post post: postService.findPostsByUid(curLoginUid)){
            curUserPidList.add(post.getPid());
        }
        for(Long similarUid: otherUserIdsWithSimilarInterest){
            int similarUidIndex = userIdList.indexOf(similarUid);
            List<Long> similarUserPraisePidList = new ArrayList<>();  // 当前某个相似用户所点赞的帖子id
            for (int postIdIndex = 0; postIdIndex < postNumber; postIdIndex++){
                if(userPraises[similarUidIndex][postIdIndex] == 1 && userPraises[curLoginUidIndex][postIdIndex] != 1){
                    // 相似用户点赞，但是当前用户没有点赞
                    Long mayLikePid = postIdList.get(postIdIndex);  // 帖子id
                    if (!curUserReadPidList.contains(mayLikePid) && !curUserPidList.contains(mayLikePid)){
                        similarUserPraisePidList.add(mayLikePid);
                    }
                }
            }
            for (Long mayLikePid: similarUserPraisePidList){
                if (!recommendPidList.contains(mayLikePid)){
                    recommendPidList.add(mayLikePid);
                }
            }
        }

        List<Long> pidListWithoutCurUser = postService.findPostIdsWithLogin(curLoginUid);  // 非当前登录用户所发布的帖子id
        for (Long pid: pidListWithoutCurUser){
            if (!recommendPidList.contains(pid)){
                recommendPidList.add(pid);
            }
        }
        return recommendPidList;
    }
}
